﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Business.Models;
using System.Data;

namespace Business.DAO
{
    public class TinNhanDAO
    {
        private ISSContext db = new ISSContext();
        public List<TinNhan> GetAll()
        {
            return db.TinNhan.ToList();
        }

        public TinNhan Get(int id)
        {
            TinNhan TinNhan = db.TinNhan.Find(id);
            return TinNhan;
        }

        public int Create(TinNhan TinNhan)
        {
            try
            {
                db.TinNhan.Add(TinNhan);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Edit(TinNhan TinNhan)
        {
            try
            {
                db.Entry(TinNhan).State = EntityState.Modified;
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Delete(int id)
        {
            try
            {
                TinNhan TinNhan = db.TinNhan.Find(id);
                db.TinNhan.Remove(TinNhan);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public List<TinNhan> GetByNhanVien(NhanVien NhanVien)
        {
            var query = from tinnhan in db.TinNhan where tinnhan.NhanVien == NhanVien select tinnhan;
            List<TinNhan> lstTinNhan = query.ToList<TinNhan>();
            return lstTinNhan;
        }
    }
}